<template>
  <div>
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: "UnLinkage",
  // inheritAttrs: false,
  props: {
    config: Object,
  },
  data() {
    return {};
  },
  computed: {
    provider() {
      return this.config.__config__.provider;
    },
    providerParamKey() {
      return this.config.__config__.providerParamKey;
    },

    consumer() {
      return this.config.__config__.consumer;
    },
    providersConfig() {
      return this.config.__config__.children.filter(({ __config__ }) =>
        this.provider.includes(__config__.renderKey)
      );

      // return providers.map(( { __config__ }) => __config__.defaultValue);
    },

    providersValue() {
      return this.providersConfig.map(
        ({ __config__ }) => __config__.defaultValue
      );
    },
    consumersConfig() {
      return this.config.__config__.children.filter(({ __config__ }) =>
        this.consumer.includes(__config__.renderKey)
      );
    },
  },
  watch: {
    // 提供者目前只支持一个
    providersValue: {
      handler(val) {
        const providersData = [];
        this.providersConfig.forEach((conf, idx) => {
          const { defaultValue, dataConsumer } = conf.__config__;

          if (dataConsumer) {
            const { props } = conf.props || {}; // 约定有 props
            for (let i = 0; i < conf[dataConsumer].length; i++) {
              const option = conf[dataConsumer][i];
              if (option[props.value || "value"] == val[idx]) {
                providersData.push(option);
              }
            }
          } else if (defaultValue === val[idx]) {
            providersData.push({ value: defaultValue });
          }
        });
        // 多个消费者
        this.consumersConfig.forEach((conf) => {
          const { providerDataKey = "value" } = conf.__config__;
          this.$set(
            conf.__config__,
            "defaultValue",
            providersData[0][providerDataKey]
          );
        });
      },
      deep: true,
    },
  },
  methods: {},
};
</script>

<style></style>
